package so.ifl.servcie.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import so.ifl.commons.tools.Constant;
import so.ifl.commons.tools.StringHelper;
import so.ifl.dao.PayRecordDao;
import so.ifl.dao.ResultInfoDao;
import so.ifl.dao.StudentInfoDao;
import so.ifl.dao.SysDao;
import so.ifl.servcie.StudentService;
import so.ifl.vo.MappingVo;
import so.ifl.vo.PayRecordVo;
import so.ifl.vo.ResultVo;
import so.ifl.vo.StudentVo;
@Service("studentService")
public class StudentServiceImpl implements StudentService {
	@Autowired
	private StudentInfoDao studentInfoDao;
	
	@Autowired
	private ResultInfoDao resultInfoDao;
	
	@Autowired
	private PayRecordDao payRecordDao;
	
	@Autowired
	private SysDao sysDao;
	
	@Override
	public List<StudentVo> queryStudentWithParamAndPagination(Map<String, Object> params, Integer offset, Integer limit) {
		return studentInfoDao.queryStudentWithParamAndPagination(params, new RowBounds(offset, limit));
	}

	@Override
	public Integer countStudentWithParam(Map<String, Object> params) {
		return studentInfoDao.countStudentWithParam(params);
	}

	@Override
	public void insertStudent(StudentVo studentVo) {
		
		studentInfoDao.insertStudent(studentVo);
		
		int studentId = sysDao.getLastInsertID();
		
		
		PayRecordVo payRecordVo = new PayRecordVo();
		payRecordVo.setStudentId(studentId);
		payRecordVo.setItemType(Constant.INIT_ENTRANCE_FEE);
		payRecordVo.setShouldPay(new Float(0));
		payRecordVo.setPayEven(new Float(0));
		
		if (null != studentVo.getStudentEntranceFee() && studentVo.getStudentEntranceFee().floatValue() > 0) {
			
			//该学员有入学初始费信息
			payRecordVo.setShouldPay(studentVo.getStudentEntranceFee());
		}
		
		if (null != studentVo.getRealPay() && studentVo.getRealPay().floatValue() > 0) {
			//实交费用
			payRecordVo.setPayEven(studentVo.getRealPay());
		}
		
		
		
		payRecordDao.insertPayRecord(payRecordVo);
		
		
		ResultVo resultVo = new ResultVo(studentId, "", "", "", "", "", "", "", "", "", "", "", "");
		
		resultInfoDao.insertResult(resultVo);
		
	}

	@Override
	public void updateStudent(StudentVo studentVo) {
		studentInfoDao.updateStudent(studentVo);
	}

	@Override
	public void deleteStudent(Integer studentId) {
		studentInfoDao.deleteStudent(studentId);
	}
	
	/**
	 * 查询成绩单
	 * create by aaron.lu
	 * create at 2012-1-18 下午9:37:58
	 * <mailto: atyuan.lu@gmail.com>
	 * @param studentId
	 * @return
	 *
	 */
	public List<ResultVo> queryResult(String studentId){
		List<ResultVo> result = new ArrayList<ResultVo>();
		if (StringHelper.isNotEmpty(studentId)) {
			Map<String, Object> params = new HashMap<String, Object>();
			
			params.put("student_id_", studentId);					
			List<ResultVo> list = resultInfoDao.queryResultOfStudent(params);
			
			if (null != list && list.size() > 0) {
				//如果已经存在成绩单则返回当前成绩单
				return list;
			}else{
				//如果没有则返回空成绩单
				ResultVo resultVo = new ResultVo();	
				resultVo.setStudentId(new Integer(studentId));
				result.add(resultVo);
			}
		}
		
		return result;
		
	}
	
	/**
	 * 保存成绩单的修改
	 * create by aaron.lu
	 * create at 2012-1-18 下午10:08:57
	 * <mailto: atyuan.lu@gmail.com>
	 * @param type
	 * @param result
	 * @param source
	 * @param studentId
	 *
	 */
	public void saveEditOfResult(String type, String result,String source, String studentId){
		Map<String, Object> params = new HashMap<String, Object>();
		
		params.put("student_id_", studentId);
		List<ResultVo> list = resultInfoDao.queryResultOfStudent(params);
		
		if (null != list && list.size() > 0) {
			ResultVo resultVo = list.get(0);//存在成绩单的则更新
			setResult(resultVo, type, result, source);
			resultInfoDao.updateResult(resultVo);
		}else{
			ResultVo resultVo = new ResultVo();
			setResult(resultVo, type, result, source);
			resultInfoDao.insertResult(resultVo);
		}
		
	}
	
	/**
	 * 保存成绩单的修改
	 * create by aaron.lu
	 * create at 2012-1-19 上午9:09:24
	 * <mailto: atyuan.lu@gmail.com>
	 * @param resultVo
	 *
	 */
	public void saveResult(ResultVo resultVo){
		Integer resultId = resultVo.getResultId();
		if (null != resultId) {
			resultInfoDao.updateResult(resultVo);
		}else{
			resultInfoDao.insertResult(resultVo);
		}
	}
	
	/**
	 * 查询付款记录
	 * create by aaron.lu
	 * create at 2012-1-19 上午10:01:55
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @param offset
	 * @param limit
	 * @return
	 *
	 */
	public List<PayRecordVo> queryPayRecordOfStudent(Map<String, Object> params, int offset, int limit){
		return payRecordDao.queryPayRecordOfStudent(params, new RowBounds(offset, limit));
	}
	
	/**
	 * 统计付款记录
	 * create by aaron.lu
	 * create at 2012-1-19 上午10:11:36
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @return
	 *
	 */
	public int countPayRecord(Map<String, Object> params){
		return payRecordDao.countPayRecord(params);
	}
	
	/**
	 * 新增付款记录
	 * create by aaron.lu
	 * create at 2012-1-19 上午10:12:37
	 * <mailto: atyuan.lu@gmail.com>
	 * @param payRecordVo
	 *
	 */
	public void insertPayRecord(PayRecordVo payRecordVo){
		payRecordDao.insertPayRecord(payRecordVo);
	}
	
	/**
	 * 更新付款记录
	 * create by aaron.lu
	 * create at 2012-1-19 上午10:13:21
	 * <mailto: atyuan.lu@gmail.com>
	 * @param payRecordVo
	 *
	 */
	public void updatePayRecord(PayRecordVo payRecordVo){
		payRecordDao.updatePayRecord(payRecordVo);
	}
	
	/**
	 * 删除付款记录
	 * create by aaron.lu
	 * create at 2012-1-19 上午10:14:17
	 * <mailto: atyuan.lu@gmail.com>
	 * @param recordId
	 *
	 */
	public void deletePayRecord(Integer recordId){
		payRecordDao.deletePayRecord(recordId);
	}
	
	/**
	 * 获取匹配信息
	 * create by aaron.lu
	 * create at 2012-3-29 下午11:33:14
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @return
	 *
	 */
	public List<MappingVo> getMapping(Map<String, Object> params){
		return sysDao.getMapping(params);
	}
	
	/**
	 * 分页查询匹配信息
	 * create by aaron.lu
	 * create at 2012-3-30 下午8:10:46
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @param limit
	 * @param offset
	 * @return
	 *
	 */
	public List<MappingVo> getMapping(Map<String, Object> params, int limit, int offset){
		return sysDao.getMapping(params, new RowBounds(offset, limit));
	}
	
	/**
	 * 统计匹配信息
	 * create by aaron.lu
	 * create at 2012-3-30 下午8:11:42
	 * <mailto: atyuan.lu@gmail.com>
	 * @param params
	 * @return
	 *
	 */
	public int countMapping(Map<String, Object> params){
		return sysDao.countMapping(params);
	}
	
	
	/**
	 * 设置成绩单
	 * create by aaron.lu
	 * create at 2012-1-18 下午10:07:53
	 * <mailto: atyuan.lu@gmail.com>
	 * @param resultVo
	 * @param type
	 * @param result
	 * @param source
	 * @return
	 *
	 */
	private ResultVo setResult(ResultVo resultVo, String type, String result,String source){
		int  type_ = Integer.valueOf(type);
		switch (type_) {
			case 1:
				resultVo.setTest1Result(result);
				resultVo.setSource1(source);
				break;
			case 2:
				resultVo.setTest2Result(result);
				resultVo.setSource2(source);
				break;	
			case 3:
				resultVo.setTest3Result(result);
				resultVo.setSource3(source);
				break;
			case 4:
				resultVo.setTest4Result(source);
				resultVo.setSource4(source);
				break;
			case 5:
				resultVo.setTest5Result(result);
				resultVo.setSource5(source);
				break;
			case 6:
				resultVo.setTest6Result(result);
				resultVo.setSource6(source);
				break;
			default:
				
				break;
		}//end of switch
		
		return resultVo;
	}
	
	

}
